#include <REGX52.H>
#include <intrins.h>

sbit scl = P0^1;
sbit sda = P0^3;

void Delay100ms()		//@11.0592MHz
{
	unsigned char i, j;

	i = 180;
	j = 73;
	do
	{
		while (--j);
	} while (--i);
}




void IIC_START(){
	
	scl = 1;
	sda = 0;
	_nop_();
	sda = 1;
	_nop_();
	sda = 0;
	_nop_();
	scl = 0;
}

void IIC_STOP(){
	
	scl = 0;
	sda = 0;
	_nop_();
	scl = 1;
	_nop_();
	sda = 1;
	_nop_();
	sda = 0;
}

void ICC_ACK(){

	char flag;
	sda = 1;
	_nop_();
	scl = 1;
	_nop_();
	flag= sda;
	_nop_();
	scl = 0;
}

void ICC_send_Btye(char datasend){

	int i = 0;
	
	for(i = 0;i < 8;i++){
		scl = 0;
		sda = datasend & 0x80;
		_nop_();
		scl = 1;
		_nop_();
		scl = 0;
		_nop_();
		datasend <<= 1;
	}
	sda = 1; //数据发送完毕，释放数据线
}



void OLED_Write_cmd(char cmd){
	
	IIC_START();
	ICC_send_Btye(0x78);
	ICC_ACK();
	ICC_send_Btye(0x00);
	ICC_ACK();
	ICC_send_Btye(cmd);
	ICC_ACK();
	IIC_STOP();
}

void OLED_Write_data(char datas){
	
	IIC_START();
	ICC_send_Btye(0x78);
	ICC_ACK();
	ICC_send_Btye(0x40);
	ICC_ACK();
	ICC_send_Btye(datas);
	ICC_ACK();
	IIC_STOP();
}

void OLED_Init(){
	
	OLED_Write_cmd(0xAE);//--display off
	OLED_Write_cmd(0x00);//---set low column address
	OLED_Write_cmd(0x10);//---set high column address
	OLED_Write_cmd(0x40);//--set start line address
	OLED_Write_cmd(0xB0);//--set page address
	OLED_Write_cmd(0x81); // contract control
	OLED_Write_cmd(0xFF);//--128
	OLED_Write_cmd(0xA1);//set segment remap
	OLED_Write_cmd(0xA6);//--normal / reverse
	OLED_Write_cmd(0xA8);//--set multiplex ratio(1 to 64)
	OLED_Write_cmd(0x3F);//--1/32 duty
	OLED_Write_cmd(0xC8);//Com scan direction
	OLED_Write_cmd(0xD3);//-set display offset
	OLED_Write_cmd(0x00);//
	OLED_Write_cmd(0xD5);//set osc division
	OLED_Write_cmd(0x80);//
	OLED_Write_cmd(0xD8);//set area color mode off
	OLED_Write_cmd(0x05);//
	OLED_Write_cmd(0xD9);//Set Pre-Charge Period
	OLED_Write_cmd(0xF1);//
	OLED_Write_cmd(0xDA);//set com pin configuartion
	OLED_Write_cmd(0x12);//
	OLED_Write_cmd(0xDB);//set Vcomh
	OLED_Write_cmd(0x30);//
	OLED_Write_cmd(0x8D);//set charge pump enable
	OLED_Write_cmd(0x14);//
	OLED_Write_cmd(0xAF);//--turn on oled panel
}

void OLED_Clear(){

	unsigned char i,j;
	
	for(i=0;i<8;i++){
		
		OLED_Write_cmd(0xB0 + i);//page0--page7
		//每个page从0列
		OLED_Write_cmd(0x00);
		OLED_Write_cmd(0x10);
		//0到127列，依次写入0，每写入数据，列地址自动偏移
		for(j = 0;j<128;j++){
			OLED_Write_data(0);
		}
	}
}

/*--  文字:  Z  --*/
/*--  宋体12;  此字体下对应的点阵为：宽x高=8x16   --*/
code char Z_up[]={0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00};
code char Z_down[]={0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00};
/*--  文字:  X  --*/
/*--  宋体12;  此字体下对应的点阵为：宽x高=8x16   --*/
code char X_up[]={0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08};
code char X_down[]={0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20};
/*--  文字:  林  --*/
/*--  宋体12;  此字体下对应的点阵为：宽x高=16x16   --*/
code char L_up[]={0x10,0x10,0xD0,0xFF,0x90,0x10,0x00,0x10,0x10,0xD0,0xFF,0xD0,0x10,0x10,0x10,0x00};
code char L_down[]={0x04,0x03,0x00,0xFF,0x00,0x11,0x08,0x04,0x03,0x00,0xFF,0x00,0x03,0x04,0x08,0x00};
/*--  文字:  6  --*/
/*--  宋体12;  此字体下对应的点阵为：宽x高=8x16   --*/
code char six1_up[]={0x00,0xE0,0x10,0x88,0x88,0x90,0x00,0x00};
code char six1_down[]={0x00,0x0F,0x11,0x20,0x20,0x20,0x1F,0x00};
/*--  文字:  6  --*/
/*--  宋体12;  此字体下对应的点阵为：宽x高=8x16   --*/
code char six2_up[]={0x00,0xE0,0x10,0x88,0x88,0x90,0x00,0x00};
code char six2_down[]={0x00,0x0F,0x11,0x20,0x20,0x20,0x1F,0x00};
/*--  文字:  6  --*/
/*--  宋体12;  此字体下对应的点阵为：宽x高=8x16   --*/
code char six3_up[]={0x00,0xE0,0x10,0x88,0x88,0x90,0x00,0x00};
code char six3_down[]={0x00,0x0F,0x11,0x20,0x20,0x20,0x1F,0x00};
/*--  调入了一幅图像：C:\Users\Administrator\Desktop\2.bmp  --*/
/*--  宽度x高度=24x32  --*/
code char pc1_1[]={0x00,0x00,0x00,0xE0,0x30,0x18,0x08,0x0C,0x0C,0x08,0x08,0x08,0x18,0x10,0x30,0xE0,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
code char pc1_2[]={0x00,0x00,0x00,0x03,0x02,0x04,0x0C,0x08,0x08,0xC8,0x78,0x08,0x0C,0x04,0x03,0x01,
0x80,0xE0,0x00,0x00,0x00,0x00,0x00,0x00};
code char pc1_3[]={0x20,0x30,0x18,0x0C,0x04,0x04,0x04,0x8C,0x78,0x0F,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
code char pc1_4[]={0x00,0x40,0x40,0x40,0x40,0x60,0x38,0x0F,0x03,0x01,0x01,0x02,0x02,0x04,0x7C,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
/*--  调入了一幅图像：C:\Users\Administrator\Desktop\1.bmp  --*/
/*--  宽度x高度=24x32  --*/
code char pc2_1[]={0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0x38,0x0C,0x04,0x04,0x0C,0x08,0x18,0xF0,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
code char pc2_2[]={0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x0C,0x08,0x98,0x78,0x08,0x0C,0x06,0x01,0x00,
0x00,0x00,0xC0,0x70,0x00,0x00,0x00,0x00};
code char pc2_3[]={0x00,0x00,0x30,0x1E,0x02,0x02,0x03,0x01,0x11,0xFF,0xC6,0x84,0x84,0x84,0x04,0x04,
0x04,0x0C,0x03,0x00,0x00,0x00,0x00,0x00};
code char pc2_4[]={0x00,0x00,0x0C,0x38,0x30,0x10,0x18,0x0E,0x03,0x01,0x00,0x00,0x00,0x00,0x21,0x31,
0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

void PlayWord(){

	int i;
	
	OLED_Write_cmd(0xB0);
	OLED_Write_cmd(0x00);
	OLED_Write_cmd(0x10);
	
	for(i = 0;i < 8; i++){
	
		OLED_Write_data(Z_up[i]);
	}
	for(i = 0;i < 8; i++){

		OLED_Write_data(X_up[i]);
	}
	for(i = 0;i < 16; i++){
	
		OLED_Write_data(L_up[i]);
	}
	for(i = 0;i < 8; i++){

		OLED_Write_data(six1_up[i]);
	}
	for(i = 0;i < 8; i++){

		OLED_Write_data(six2_up[i]);
	}
	for(i = 0;i < 8; i++){

		OLED_Write_data(six3_up[i]);
	}
	
	OLED_Write_cmd(0xB1);
	OLED_Write_cmd(0x00);
	OLED_Write_cmd(0x10);
	
	for(i = 0;i < 8; i++){
	
		OLED_Write_data(Z_down[i]);
	}
	for(i = 0;i < 8; i++){
	
		OLED_Write_data(X_down[i]);

	}
	for(i = 0;i < 16; i++){
	
		OLED_Write_data(L_down[i]);
	}
	for(i = 0;i < 8; i++){
	
		OLED_Write_data(six1_down[i]);
	}
	for(i = 0;i < 8; i++){

		OLED_Write_data(six2_down[i]);
	}
	for(i = 0;i < 8; i++){
	
		OLED_Write_data(six3_down[i]);
	}
}

void PlayPct(){

	int i;
	while(1){
		
	PlayWord();
		
	OLED_Write_cmd(0xB3);
	OLED_Write_cmd(0x00);
	OLED_Write_cmd(0x11);
	
	for(i = 0;i < 24;i++){
		
		OLED_Write_data(pc1_1[i]);
	}

	OLED_Write_cmd(0xB4);
	OLED_Write_cmd(0x00);
	OLED_Write_cmd(0x11);
	
	for(i = 0;i < 24;i++){
		
		OLED_Write_data(pc1_2[i]);
	}
	
	OLED_Write_cmd(0xB5);
	OLED_Write_cmd(0x00);
	OLED_Write_cmd(0x11);
	
	for(i = 0;i < 24;i++){
		
		OLED_Write_data(pc1_3[i]);
	}
	
	OLED_Write_cmd(0xB6);
	OLED_Write_cmd(0x00);
	OLED_Write_cmd(0x11);
	
	for(i = 0;i < 24;i++){
		
		OLED_Write_data(pc1_4[i]);
	}
	
	Delay100ms();

	
	PlayWord();
		
	OLED_Write_cmd(0xB3);
	OLED_Write_cmd(0x00);
	OLED_Write_cmd(0x11);
	
	for(i = 0;i < 24;i++){
		
		OLED_Write_data(pc2_1[i]);
	}

	OLED_Write_cmd(0xB4);
	OLED_Write_cmd(0x00);
	OLED_Write_cmd(0x11);
	
	for(i = 0;i < 24;i++){
		
		OLED_Write_data(pc2_2[i]);
	}
	
	OLED_Write_cmd(0xB5);
	OLED_Write_cmd(0x00);
	OLED_Write_cmd(0x11);
	
	for(i = 0;i < 24;i++){
		
		OLED_Write_data(pc2_3[i]);
	}
	
	OLED_Write_cmd(0xB6);
	OLED_Write_cmd(0x00);
	OLED_Write_cmd(0x11);
	
	for(i = 0;i < 24;i++){
		
		OLED_Write_data(pc2_4[i]);
	}
	
	Delay100ms();
}
}
void main(){

	
	
	OLED_Init();

	OLED_Write_cmd(0x20);
	OLED_Write_cmd(0x02);
	OLED_Clear();
	PlayPct();
	
	
	while(1);
}
